﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using ZXing;
using ZXing.QrCode;

using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Tool
{
    public static class Tools
    {
        /// <summary>
        /// 对象转JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetString<T>(T t)
        {
            String str = Newtonsoft.Json.JsonConvert.SerializeObject(t);
            return str;
        }
        /// <summary>
        /// JSON转对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T FromJsonStr<T>(string str)
        {
            T t = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str);
            return t;
        }
        /// <summary>
        /// MD5
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static String MD5Encrypt(String content)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(content);
            bytes = md5.ComputeHash(bytes);
            md5.Clear();
            string ret = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
            }
            return ret.PadLeft(32, '0').ToUpper();
        }
        /// <summary>
        /// 获取本地IP
        /// </summary>
        /// <returns></returns>
        public static string GetIPAddress()
        {
            string ip = "127.0.0.1";
            string HostName = Dns.GetHostName(); //得到主机名  
            IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
            for (int i = 0; i < IpEntry.AddressList.Length; i++)
            {
                //从IP地址列表中筛选出IPv4类型的IP地址  
                //AddressFamily.InterNetwork表示此IP为IPv4,  
                //AddressFamily.InterNetworkV6表示此地址为IPv6类型  
                if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    ip = IpEntry.AddressList[i].ToString();
                }
            }
            return ip;
        }
        /// <summary>
        /// Byte转十进制String{0x14 0x12}/{20 18}=》2018
        /// </summary>
        /// <param name="byteArr"></param>
        /// <returns></returns>
        public static string ByteArrToDecString(byte[] byteArr)
        {
            string retStr = "";
            for (int i = 0; i < byteArr.Length; i++)
            {
                retStr += byteArr[i] < 10 ? "0" + byteArr[i].ToString() : byteArr[i].ToString();

            }
            return retStr;
        }
        /// <summary>
        /// Byte转十六进制String{0x14 0x12}/{20 18}=》1412
        /// </summary>
        /// <param name="byteArr"></param>
        /// <returns></returns>
        public static string ByteArrToHexString(byte[] byteArr)
        {
            string retStr = "";
            for (int i = 0; i < byteArr.Length; i++)
            {
                retStr += byteArr[i].ToString("x2");
            }
            return retStr;
        }
        /// <summary>
        /// Byte转Char
        /// </summary>
        /// <param name="byteArr"></param>
        /// <returns></returns>
        public static string ByteArrToCharString(byte[] byteArr)
        {
            string retStr = "";
            for (int i = 0; i < byteArr.Length; i++)
            {
                retStr += Convert.ToChar(byteArr[i]);
            }
            return retStr;
        }
        /// <summary>
        /// string转16进制byte[]  2018 =>{0x20 0x18}/{32 24}
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] StringToHexByteArr(string str)
        {
            byte[] byteArr = new byte[str.Length / 2];
            for (int i = 0; i < str.Length; i = i + 2)
            {
                string strSpit = str.Substring(i, 2);
                byteArr[i / 2] = Convert.ToByte(strSpit, 16);
            }
            return byteArr;
        }
        /// <summary>
        /// string转十进制byte[]  2018 =>{0x14 0x12}/{20 18}
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] StringToDecByteArr(string str)
        {
            byte[] byteArr = new byte[str.Length / 2];
            for (int i = 0; i < str.Length; i = i + 2)
            {
                string strSpit = str.Substring(i, 2);
                byteArr[i / 2] = Convert.ToByte(strSpit, 10);
            }
            return byteArr;
        }
        /// <summary>
        /// string转十进制的ascll byte[]  2018 =>{0x32,0x30,0x31,0x38}
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] OneStringToDecByteArr(string str)
        {
            byte[] byteArr = new byte[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                string strSpit = str.Substring(i, 1);
                byteArr[i] = Convert.ToByte(Convert.ToByte(strSpit, 10) + 48);
            }
            return byteArr;
        }


        /// <summary>
        /// string转化为图片
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static Bitmap GenerateQRCode(string text)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions();
            options.DisableECI = true;
            //设置内容编码
            options.CharacterSet = "UTF-8";
            //设置二维码的宽度和高度
            options.Width = 300;
            options.Height = 300;
            //设置二维码的边距,单位不是固定像素
            options.Margin = 1;
            writer.Options = options;
            Bitmap map = writer.Write(text);
            return map;
        }

        /// <summary>
        /// 对象序列化为XML
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static string XMLSerialize<T>(T entity)
        {
            StringBuilder buffer = new StringBuilder();
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (TextWriter writer = new StringWriter(buffer))
            {
                serializer.Serialize(writer, entity);
            }
            return buffer.ToString();

        }

        /// <summary>
        /// XMLString反序列化为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xmlString"></param>
        /// <returns></returns>
        public static T DeXMLSerialize<T>(string xmlString)
        {
            T cloneObject = default(T);

            StringBuilder buffer = new StringBuilder();
            buffer.Append(xmlString);

            XmlSerializer serializer = new XmlSerializer(typeof(T));

            using (TextReader reader = new StringReader(buffer.ToString()))
            {
                Object obj = serializer.Deserialize(reader);
                cloneObject = (T)obj;
            }

            return cloneObject;
        }


    }
}
